/*
 * Copyright (c) 2011, Yawning <yawninglol at gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   * Neither the name of the Mew Developers nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package yawning.mew.sim;

public class SimulationEventTargetStun extends SimulationEvent {
	private SimulationTarget mTarget;

	private boolean mIsImmune;

	private int mNrControlledStunDRs;
	private double mControlledStunDRExpireTime;

	private int mNrChargeStunDRs;
	private double mChargeStunDRExpireTime;

	private boolean mIsLogging;
	private SimulationLog mLog;

	public SimulationEventTargetStun(SimulationEngine su, SimulationTarget target) {
		super(su);

		mIsLogging = mSimEngine.isLogging();
		mLog = mSimEngine.getLog();

		mTarget = target;
		mIsImmune = !mTarget.getTarget().getIsStunnable();
	}

	public void controlledStun(double duration) {
		if (mIsImmune) {
			if (mIsLogging) mLog.log("Stun: %s IMMUNE\n", mTarget.getTarget().getName());
			return;
		}

		// Check diminishing returns expiration.
		if (mControlledStunDRExpireTime <= mSimEngine.getSimTime()) {
			mNrControlledStunDRs = 0;
		}

		switch (mNrControlledStunDRs) {
		case 0: break;
		case 1: duration = duration / 2; break;
		case 2: duration = duration / 4; break;
		default:
			if (mIsLogging) mLog.log("Stun: %s IMMUNE (DR)\n", mTarget.getTarget().getName());
			return; // Immune.
		}

		// Setup the DR.
		mNrControlledStunDRs++;
		mControlledStunDRExpireTime = mSimEngine.getSimTime() + 15;

		stun(duration);
	}

	public void chargeStun(double duration) {
		if (mIsImmune) {
			if (mIsLogging) mLog.log("Stun: %s IMMUNE\n", mTarget.getTarget().getName());
			return;
		}

		// Check diminishing returns expiration.
		if (mChargeStunDRExpireTime <= mSimEngine.getSimTime()) {
			mNrChargeStunDRs = 0;
		}

		switch (mNrChargeStunDRs) {
		case 0: break;
		case 1: duration = duration / 2; break;
		case 2: duration = duration / 4; break;
		default:
			if (mIsLogging) mLog.log("Stun: %s IMMUNE (DR)\n", mTarget.getTarget().getName());
			return; // Immune.
		}

		// Setup the DR.
		mNrChargeStunDRs++;
		mChargeStunDRExpireTime = mSimEngine.getSimTime() + 15;

		stun(duration);
	}

	private void stun(double duration) {
		if (mIsLogging) mLog.log("Stun: %s Duration: %.2f\n", mTarget.getTarget().getName(), duration);

		// Handle the cast timer.
		if (mTarget.getTargetCastEv().isUp())
			mTarget.getTargetCastEv().stun(duration);

		if (!isUp())
			onStunStart();

		// Stun the mob.
		schedule(duration);
	}

	@Override
	public void onExpire() {
		if (mIsLogging) mLog.log("Stun Expiration: %s\n", mTarget.getTarget().getName());

		onStunStop();
	}

	private boolean mSwingWasEnabled;
	private boolean mYellowWasEnabled;
	private boolean mMagicWasEnabled;

	private void onStunStart() {
		if (mTarget.getTargetSwingEv().isUp()) {
			mSwingWasEnabled = true;
			mTarget.getTargetSwingEv().stop();
		}

		if (mTarget.getTargetYellowEv().isUp()) {
			mYellowWasEnabled = true;
			mTarget.getTargetYellowEv().pause();
		}

		if (mTarget.getTargetMagicEv().isUp()) {
			mMagicWasEnabled = true;
			mTarget.getTargetMagicEv().pause();
		}
	}

	private void onStunStop() {
		if (mSwingWasEnabled == true) mTarget.getTargetSwingEv().start();
		if (mYellowWasEnabled == true) mTarget.getTargetYellowEv().start();
		if (mMagicWasEnabled == true) mTarget.getTargetMagicEv().start();
	}
}
